import { getPermissions } from "@/api/permission";
const state = {
  permissions: {}
};

// actions
const actions = {
  async init({ dispatch }) {
    const permissions = await getPermissions();
    await dispatch("set", permissions);
  },

  set({ state, dispatch }, permissions) {
    state.permissions = permissions;
    // 持久化
    return dispatch(
      "db/set",
      {
        dbName: "sys",
        path: "menu.permissions",
        user: true,
        value: permissions
      },
      { root: true }
    );
  },

  async load({ state, dispatch }) {
    const permissions = dispatch(
      "db/get",
      {
        dbName: "sys",
        path: "menu.asideMenus",
        defaultValue: {},
        user: true
      },
      { root: true }
    );
    state.permissions = permissions;
    return permissions;
  }
};

export default {
  namespaced: true,
  state,
  actions
};
